<?php
namespace servers\cyf;

use Elastic\Elasticsearch\ClientBuilder;

class EsServer
{
    protected $client;
    //连接es
    public function __construct()
    {
        $host=['127.0.0.1:9200'];
        $client = ClientBuilder::create()
            ->setHosts($host)
            ->build();

        $this->client = $client;
    }

    /**
     * 添加es
     * @param $index
     * @param $array
     * @throws \Elastic\Elasticsearch\Exception\ClientResponseException
     * @throws \Elastic\Elasticsearch\Exception\MissingParameterException
     * @throws \Elastic\Elasticsearch\Exception\ServerResponseException
     */
    public function createEs($index,$array)
    {
        $params = [
            'index'=>$index ,
            'id'=>$array['id'],
            'body' =>$array
        ];
        $res = $this->client->index($params);
        print_r($res);
    }

    public function searchEs($index,$search)
    {
        $params = [
            'index' => $index,
            'body'  => [
                'query' => [
                    'match' => [
                        'testField' => $search
                    ]
                ]
            ]
        ];

        return $this->client->search($params);
    }


}